﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Data;

namespace School.Pages.Bus
{

    public partial class BasicPricing : System.Web.UI.Page
    {
        private SchoolEntities db = new SchoolEntities();
        DataRow dr;
        DataRow drp;
        DataTable dt;
        DataTable dtp;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1200) == true)
                {
                    if (!IsPostBack)
                    {


                        ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                        ddlBind(ddlcycle, School.Service.Bus.GetCycle().ToList());
                        ddlbus.Items.Insert(0, new ListItem("اختيار", "-1"));
                        if (ddCamp1.SelectedIndex > 0)
                        {
                            ddlBind(ddlbus, School.Service.Bus.GetBusByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString())).ToList());
                            LbDistrictBind();
                        }
                    }
                    lbltestBus.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void LbDistrictBind()
        {
            LbDistrict.DataSource = School.Common.Structure.GetDistrictByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString()));
            LbDistrict.Items.Clear();
            LbDistrict.DataBind();
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }
        protected void ddCamp1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddCamp1.SelectedIndex > 0)
            {
                ddlBind(ddlbus, School.Service.Bus.GetBusByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString())).ToList());
                LbDistrictBind();
            }
            plbus.Visible = false;
        }
        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            plbus.Visible = true;
            gvstationBind();
            gvpriceBind();
        }
        private void gvpriceBind()
        {
            IEnumerable<SRV_Bus_Station_Price> q = School.Service.Bus.GetstationPricebycamp(Convert.ToInt32(ddCamp1.SelectedValue), Convert.ToInt32(ddlbus.SelectedValue));
            DataTable(out dt, out dtp);
            for (int i = 0; i < LbDistrict.Items.Count; i++)
            {
                if (LbDistrict.Items[i].Selected)
                {
                    q = q.Where(a => a.District_Id == Convert.ToInt32(LbDistrict.Items[i].Value));
                    foreach (var t in q)
                    {
                        drp = dtp.NewRow();
                        drp["Id"] = t.Id;
                        drp["Campus_Id"] = t.Campus_Id;
                        drp["Station_Id"] = t.Station_Id;
                        drp["Bus_Id"] = t.Bus_Id;
                        drp["Cycle_Id"] = t.Cycle_Id;
                        drp["District_Id"] = t.District_Id;
                        drp["Price"] = t.Price;
                        var name = (from s in db.SRV_Bus_Station where t.Station_Id == s.Id select new { s.Name }).SingleOrDefault();
                        drp["Name"] = name.Name.ToString();
                        dtp.Rows.Add(drp);
                    }
                }

            }
            gvprice.DataSource = dtp;
            gvprice.DataBind();
        }
        private void gvstationBind()
        {

            IEnumerable<SRV_Bus_Station> query = School.Service.Bus.GetStationByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString()),Convert.ToInt32(ddlbus.SelectedValue)); ;
            DataTable(out dt, out dtp);
            for (int i = 0; i < LbDistrict.Items.Count; i++)
            {
                if (LbDistrict.Items[i].Selected)
                {
                    query = query.Where(s => s.District_Id == Convert.ToInt32(LbDistrict.Items[i].Value));
                    foreach (var item in query)
                    {
                        dr = dt.NewRow();
                        dr["Id"] = item.Id;
                        dr["Sta_Code"] = item.Sta_Code;
                        dr["Name"] = item.Name;
                        dr["Campus_Id"] = item.Campus_Id;
                        dr["District_Id"] = item.District_Id;
                        dt.Rows.Add(dr);
                    }

                }
            }
            gvstation.DataSource = dt;
            gvstation.DataBind();
        }

        private static void DataTable(out DataTable dt, out DataTable dtp)
        {
            dt = new DataTable();

            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Sta_Code", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Campus_Id", typeof(int));
            dt.Columns.Add("District_Id", typeof(int));
            dtp = new DataTable();

            dtp.Columns.Add("Id", typeof(int));
            dtp.Columns.Add("Campus_Id", typeof(int));
            dtp.Columns.Add("Station_Id", typeof(int));
            dtp.Columns.Add("Bus_Id", typeof(int));
            dtp.Columns.Add("Cycle_Id", typeof(int));
            dtp.Columns.Add("District_Id", typeof(int));
            dtp.Columns.Add("Price", typeof(decimal));
            dtp.Columns.Add("Name", typeof(string));
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            bool re = false;
            foreach (GridViewRow Row in gvstation.Rows)
            {
                TextBox txtP = (TextBox)gvstation.Rows[Row.RowIndex].FindControl("txtprice");
                int Id = Convert.ToInt32(gvstation.DataKeys[Row.RowIndex].Value.ToString());
                int District_Id = Convert.ToInt32(gvstation.DataKeys[Row.RowIndex].Values["District_Id"].ToString());

                if (Convert.ToInt32(txtP.Text) != 0)
                {
                    re = School.Service.Bus.AddBus_Station_Price(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Id, Convert.ToInt32(ddlbus.SelectedValue.ToString()), Convert.ToInt32(ddlcycle.SelectedValue.ToString()), District_Id, Convert.ToDecimal(txtP.Text));

                }
            }
            if (re == true)
            {
                lbltestBus.Text = "تمت الاضافة بنجاح";
                gvstationBind();
                gvpriceBind();
            }
            else
            {
                lbltestBus.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }

        protected void gvprice_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvprice.EditIndex = e.NewEditIndex;
            gvpriceBind();
        }

        protected void gvprice_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvprice.EditIndex = -1;
            gvpriceBind();
        }

        protected void gvprice_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvprice.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Service.Bus.DeleteStationprice(Id);
            if (re == true)
            {
                lbltestBus.Text = "تم الحذف بنجاح";
                gvpriceBind();
                gvstationBind();
            }
            else
            {
                lbltestBus.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }

        protected void gvprice_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvprice.DataKeys[e.RowIndex].Value.ToString());
            TextBox price = (TextBox)gvprice.Rows[e.RowIndex].FindControl("txtbprice");
            bool re = School.Service.Bus.UpdateStationPrice(Id, Convert.ToDecimal(price.Text));
            if (re == true)
            {
                lbltestBus.Text = "تم التحديث بنجاح";
                gvprice.EditIndex = -1;
                gvpriceBind();
            }
            else
            {
                lbltestBus.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }

        protected void gvprice_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvprice.PageIndex = e.NewPageIndex;
            gvpriceBind();
        }

        protected void gvprice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }


    }
}